<template>
  <!--
      作者：luoyiming
      时间：2020-06-01
      描述：插件中心-文章-添加
    -->
  <div class="product-add pb50" v-loading="loading">
    <!--添加文章-->
    <el-form size="small" :model="form" ref="form" :rules="rules" label-width="100px">
      <div class="common-form">内容</div>
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入标题" class="max-w460"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="form.desc" type="textarea" :rows="8" :maxlength="300" show-word-limit placeholder="请输入描述" class="max-w460"></el-input>
      </el-form-item>
      <el-form-item label="内容详情">
        <div class="edit_container">
          <Uediter v-if="!loading" v-model="form.content" :text="ueditor.text" :config="ueditor.config" ref="ue">
          </Uediter>
        </div>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
  import InfoApi from '@/api/info.js';
  import Uediter from '@/components/UE.vue';
  export default {
    components: {
      /*编辑器*/
      Uediter,
    },
    data() {
      return {
        /*是否加载完成*/
        loading: true,
        /*富文本配置*/
        ueditor: {
          text: '',
          config: {
            initialFrameWidth: 800,
            initialFrameHeight: 500
          }
        },
        /*form表单数据*/
        form: {
          info_id: 0,
          menu_id: 0,
          title: '',
          content: '',
          desc: ''
        },
        /*验证规则*/
        rules: {
          title: [{
            required: true,
            message: '请输入标题',
            trigger: 'blur',
          }],
        }
      };
    },
    created() {
      this.form.menu_id = this.$route.query.menu_id * 1;
      /*获取列表*/
      this.getDetail();
    },
    methods: {
      /*获取详情*/
      getDetail() {
        let self = this;
        let Params = {};
        Params.menu_id = this.form.menu_id;
        InfoApi.toAddInfo(Params, true)
          .then(res => {
            if (res.data.model) {
              self.ueditor.text = res.data.model.content;
              self.form = res.data.model;
            }
            self.loading = false;
          })
          .catch(error => {

          });
      },
      /*添加文章*/
      onSubmit() {
        let self = this;
        let form = self.form;
        self.form.content = this.$refs.ue.getUEContent();
        self.$refs.form.validate(valid => {
          if (valid) {
            self.loading = true;
            InfoApi.editInfo(form, true)
              .then(data => {
                self.loading = false;
                self.$message({
                  message: data.msg,
                  type: 'success'
                });
                self.$router.push('/info/info/index');
              })
              .catch(error => {
                self.loading = false;
              });
          }
        });
      },
      /*取消添加，返回文章列表*/
      cancelFunc() {
        this.$router.push({
          path: '/info/info/index'
        });
      }
    }
  };
</script>

<style>
  .edit_container {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
  }

  .ql-editor {
    height: 400px;
  }
</style>
